import config from '../config'
import position from '../service/position'
export default abstract class canvasAbstract{
    public models:IModel[] = [];
    abstract num():number;
    abstract model():IModelAbstract | BulletIModelAbstract;
    abstract render():void;
    constructor(
        protected name:string,
        protected app = document.querySelector('#app') as HTMLDivElement,
        protected el = document.createElement('canvas'),
        public ctx = el.getContext('2d')!
    ){
        this.createCanvas();
    }

    protected createCanvas(){
        this.el.width = config.canvas.width;
        this.el.height = config.canvas.height;
        this.el.setAttribute('name',this.name);
        this.app.insertAdjacentElement('beforeend',this.el);
    }

    protected createModels(){
        position.getCollection(this.num()).forEach(position => {
            const model = this.model() as IModelAbstract;
            const instance = new model(position.x,position.y);
            this.models.push(instance);
        });
    }

    public renderModels(){
        this.ctx.clearRect(0,0,config.canvas.width,config.canvas.height);
        this.models.forEach(model => model.render())
    }

    public removeModel(model:IModel){
        this.models = this.models.filter(m => m != model);
    }
}